#include "StdAfx.h"
#include "Windows.h"
#include "VoiceNames.h"

VoiceNames::VoiceNames(void)
{
}

VoiceNames::~VoiceNames(void)
{
}

LONG VoiceNames::GetNames(std::vector<std::wstring>& destination)
{
	HKEY localMachine = HKEY_LOCAL_MACHINE;
	HKEY groupKey;
	std::wstring tokenGroup(L"SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens");
	LONG result = RegOpenKeyEx(localMachine, tokenGroup.c_str(), 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &groupKey);
	if(result==ERROR_SUCCESS)
	{
		int idx = 0;
		do
		{
			DWORD size = 100;
			WCHAR buf[100];			
			result = RegEnumKeyEx(groupKey, idx++, 	buf, &size, 0, NULL, 0, NULL);
			if(result!=ERROR_SUCCESS)
			{
				break;
			}

			HKEY voiceKey;
			result = RegOpenKeyEx(groupKey, buf, 0, KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS, &voiceKey);
			if(result==ERROR_SUCCESS)
			{
				HKEY attributesKey;
				result = RegOpenKeyEx(voiceKey, L"Attributes", 0, KEY_QUERY_VALUE, &attributesKey);
				if(result==ERROR_SUCCESS)
				{
					WCHAR value[1000];				
					size = 1000;
					result = RegGetValue(attributesKey, NULL, L"Name", RRF_RT_ANY, NULL, (PVOID)value, &size);
					if(result==ERROR_SUCCESS)
					{
						destination.push_back(std::wstring(value));
					}
					else
					{
						break;
					}
				}
				else
				{
					break;
				}
			}
			else
			{
				break;
			}
		} while(result==ERROR_SUCCESS);		
	}
	return result;	
}